Skip to content

fix: support import module in removeUnusedImports (GJF 1.30.0) - #3008

Open
arimu1 wants to merge 4 commits into
diffplug:mainfrom
arimu1:fix/2890-module-import-remove-unused
Open

fix: support import module in removeUnusedImports (GJF 1.30.0)#3008
arimu1 wants to merge 4 commits into
diffplug:mainfrom
arimu1:fix/2890-module-import-remove-unused

Conversation

@arimu1

@arimu1 arimu1 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Problem

java { removeUnusedImports() } / <removeUnusedImports/> fails on sources that use Java's import module (JEP 511), e.g.:

import module java.base;

class ModuleImportTest {
  void test() {
    UUID.randomUUID();
  }
}

with:

LINE_UNDEFINED removeUnusedImports(java.lang.IllegalArgumentException)
object of type com.sun.tools.javac.tree.JCTree$JCModuleImport is not an instance of com.sun.tools.javac.tree.JCTree$JCImport

Root cause: default google-java-format was still 1.28.0. Module-import support landed in google-java-format 1.30.0 (google/google-java-format#1213 / #1297), which skips JCModuleImport in RemoveUnusedImports instead of casting every import tree to JCImport.

Fixes #2890

Solution

  • Bump default google-java-format 1.28.01.30.0 (first release with import module support)
  • Require at least 1.30.0 when Spotless runs on JVM 25+ (Jvm.Support.addMin)
  • Align gradle/libs.versions.toml compile coordinate with the new default
  • Regression tests for module imports (JDK 25+) and the new JVM 25 minimum

Users who pin an older GJF version on JVM 25 get a clear lint instead of a ClassCastException mid-format.

Testing

# JDK 21 (module-import parse tests skipped — javac < 25 cannot parse import module)
./gradlew :testlib:test \
  --tests 'com.diffplug.spotless.java.RemoveUnusedImportsStep_withGoogleJavaFormatTest' \
  --tests 'com.diffplug.spotless.java.GoogleJavaFormatStepTest'
# 16 tests, 4 skipped — all green

# JDK 25
JAVA_HOME=…/jdk-25 ./gradlew :testlib:test \
  --tests 'com.diffplug.spotless.java.RemoveUnusedImportsStep_withGoogleJavaFormatTest' \
  --tests 'com.diffplug.spotless.java.GoogleJavaFormatStepTest'
# moduleImports + versionBelowMinOnJava25IsNotAllowed green

Platform: macOS aarch64, Temurin 21.0.12 + Temurin 25.0.4

Checklist

arimu1 and others added 4 commits August 1, 2026 14:29
Bump default google-java-format 1.28.0 -> 1.30.0 (first release that
handles JCModuleImport) and require at least 1.30.0 on JVM 25+.

Fixes diffplug#2890
…t-remove-unused

# Conflicts:
#	CHANGES.md
#	plugin-gradle/CHANGES.md
#	plugin-maven/CHANGES.md
…t-remove-unused

# Conflicts:
#	plugin-gradle/CHANGES.md
#	plugin-maven/CHANGES.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

java.removeUnusedImports does not handle import module correctly

2 participants